package com.flyee.viewer.menu
{
	import com.flyee.model.MenuData;
	
	
	public class MainMenuXMLParser
	{
		/**
		 *  
		 */		
		private var mainItems:Array = null;
		private var hypoItems:Array = null;
		
		public function MainMenuXMLParser()
		{
			mainItems = new Array();
			hypoItems = new Array();
		}
		
		public function parse(mess:String):void{
			var xml:XML = new XML(mess);
			var leng:int = xml.child("items").length(); 
			for(var i:int = 0;i < leng;i++){
				var dataModel:MenuData = new MenuData();
				dataModel.itemId = parseInt(xml.child("items")[i].attribute("id"));
				dataModel.itemIcon = String(xml.child("items")[i].attribute("icon"));
				dataModel.itemName = String(xml.child("items")[i].child("name"));
				dataModel.itemKeyword = String(xml.child("items")[i].child("keyword"));
				
				var xmlList:XMLList = xml.child("items")[i].child("item");
				var len:int = xmlList.length();
				var models:Array = new Array(len);
				for(var j:int = 0;j < len;j++){
					var model:MenuData = new MenuData();
					model.itemIcon = String(xmlList[j].attribute("icon"));
					model.itemName = String(xmlList[j].child("name"));
					model.itemId = parseInt(xmlList[j].attribute("id"));
					model.itemKeyword = String(xmlList[j].child("keyword"));
					model.isLeaf = false;
					models[j] = model;
				}
				if(len > 0){
					dataModel.isLeaf = true;
					dataModel.childNum = len;
				}
				mainItems.push(dataModel);
				hypoItems.push(models);
			}
		}
		
		public function getAllMainItems():Array{
			return mainItems;
		}
		
		public function getMainItemLength():int{
			return mainItems.length;
		}
		
		public function getMainItemsAt(tag:int):MenuData{
			if(tag > 0 && tag <= mainItems.length){
				var model:MenuData = MenuData(mainItems[tag]);
				return model;
			}
			return null; 
		}
		
		public function getHypoItemsAt(tag:int):Array{
			if(tag > 0 && tag <= hypoItems.length){
				var temp:Array = new Array();
				for(var i:int = 0;hypoItems[tag].length;i++){
					temp.push(hypoItems[tag][i]);
				}
				return temp;
			}
			return null;
		}
		
		public function getAllHypoItems():Array{
			return hypoItems;
		}
		
		public function clear():void{
			mainItems.length = 0;
			mainItems = null;
			hypoItems.length = 0;
			hypoItems = null;
		}
	}
}